Grokking Microsoft Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Microsoft Easy Problems
Buddy Strings (easy)
Solution: Buddy Strings
Can Place Flowers (easy)
Solution: Can Place Flowers
Reverse a LinkedList (easy)
Solution: Reverse a LinkedList
Maximum Number of Balloons (easy)
Solution: Maximum Number of Balloons
Binary Tree Path Sum (easy)
Solution: Binary Tree Path Sum
Pair with Target Sum (easy)
Solution: Pair with Target Sum
Buy Two Chocolates (easy)
Solution: Buy Two Chocolates
Root Equals Sum of Children (easy)
Solution: Root Equals Sum of Children
Excel Sheet Column Title (easy)
Solution: Excel Sheet Column Title
Merge Strings Alternately (easy)
Solution: Merge Strings Alternately
Min Cost Climbing Stairs (easy)
Solution: Min Cost Climbing Stairs
Plus One (easy)
Solution: Plus One
Intersection of Two Arrays (easy)
Solution: Intersection of Two Arrays
Find the Index of the First Occurrence in a String (easy)
Solution: Find the Index of the First Occurrence in a String
Merge Two Sorted Lists (easy)
Solution: Merge Two Sorted Lists
Nth Digit (medium)
Solution: Nth Digit
Restore IP Addresses (medium)
Solution: Restore IP Addresses
Find Original Array From Doubled Array (medium)
Solution: Find Original Array From Doubled Array
Reverse Words in a String II (medium)
Solution: Reverse Words in a String II
Longest Arithmetic Subsequence (medium)
Solution: Longest Arithmetic Subsequence
Matchsticks to Square (medium)
Solution: Matchsticks to Square
Score of Parentheses (medium)
Solution: Score of Parentheses
Maximum Length of a Concatenated String with Unique Characters (medium)
Solution: Maximum Length of a Concatenated String with Unique Characters
Inorder Successor in BST (medium)
Solution: Inorder Successor in BST
Distribute Coins in Binary Tree (medium)
Solution: Distribute Coins in Binary Tree
Spiral Matrix (medium)
Solution: Spiral Matrix
Delete Node in a BST (medium)
Solution: Delete Node in a BST
Sum of Subarray Minimums (medium)
Solution: Sum of Subarray Minimums
Rotate Image (medium)
Solution: Rotate Image
Combinations (medium)
Solution: Combinations
Maximum Level Sum of a Binary Tree (medium)
Solution: Maximum Level Sum of a Binary Tree
Multiply Strings (medium)
Solution: Multiply Strings
Best Time to Buy and Sell Stock II (medium)
Solution: Best Time to Buy and Sell Stock II
Largest Palindromic Number (medium)
Solution: Largest Palindromic Number
Remove Duplicate Letters (medium)
Solution: Remove Duplicate Letters
Next Permutation (medium)
Solution: Next Permutation
132 Pattern (medium)
Solution: 132 Pattern
Valid Sudoku (medium)
Solution: Valid Sudoku
Number of Islands (medium)
Solution: Number of Islands
Buildings With an Ocean View (medium)
Solution: Buildings With an Ocean View
Regular Expression Matching (hard)
Solution: Regular Expression Matching
First Missing Positive (hard)
Solution: First Missing Positive
Largest Rectangle in Histogram (hard)
Solution: Largest Rectangle in Histogram
Sliding Window Maximum (hard)
Solution: Sliding Window Maximum
Count Unique Characters of All Substrings of a Given String (hard)
Solution: Count Unique Characters of All Substrings of a Given String
Word Ladder (hard)
Solution: Word Ladder
Word Ladder II (hard)
Solution: Word Ladder II
Number of Flowers in Full Bloom (hard)
Solution: Number of Flowers in Full Bloom
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Solution: Excel Sheet Column Title

Problem Statement

Given a positive integer n, return the string referring to the corresponding column title in the Excel sheet.

Excel titles are composed of uppercase letters, where "A" denotes the first column, "B" the second, up to "Z" for the 26th column. After "Z", the titles continue with two-letter combinations starting with "AA", "AB", and so on.

Examples

  1. Example 1

    • Input: 700
    • Expected Output: "ZX"
    • Justification: The number 700 corresponds to the column title "ZX" in Excel, where "Z" is the 26th letter, and "X" follows in the sequence, indicating 26*26 + 24.
  2. Example 2

    • Input: 52
    • Expected Output: "AZ"
    • Justification: The number 52 translates to "AZ", where "A" denotes the first cycle of 26, and "Z" is the 26th letter.
  3. Example 3

    • Input: 705
    • Expected Output: "AAC"
    • Justification: For the number 705, the column title is "AAC", which represents 26*26 + 1*26 + 3, indicating two cycles through the alphabet plus three letters into the next cycle.

Solution

To solve this problem, we adopt a method that iteratively determines each letter in the column title by dividing the input number by 26 (the number of letters in the English alphabet) and finding the remainder. This approach works because each letter in the title can be seen as a digit in a base-26 numeral system, where "A" represents 1, "B" represents 2, and so on, up to "Z" representing 26. However, since there is no "zero" in this system, we adjust by treating any remainder of 0 as 26 and decrementing the dividend accordingly before the next iteration. This method is effective because it directly maps the numerical value to its equivalent alphabetical representation in a straightforward, iterative manner, mimicking the process of converting numbers to a different base, which is exactly what the problem requires.

Step-by-step Algorithm

  1. Initialize an empty string named result which will be used to build the column title in reverse order.

  2. Loop until the input number n is greater than 0. This loop will repeatedly divide n by 26 to process each digit in the base-26 number system equivalent.

  3. Inside the loop, first decrement n by 1. Excel's column numbering starts at 1 (not 0), so we adjust n to fit a 0-indexed system. This adjustment allows for a straightforward mapping of numbers to letters ('A' to 'Z').

  4. Calculate the current letter by finding the remainder of n % 26. This step determines the current "digit" in the base-26 number. Then, map this number to its corresponding letter in the alphabet. Since 'A' corresponds to 0 after our adjustment, add the ASCII code of 'A' to the remainder to get the correct letter.

  5. Prepend this letter to result. Since we are processing the digits in reverse order (from least significant to most significant), we add each new character to the beginning of result.

  6. Divide n by 26 to move to the next most significant digit.

  7. After exiting the loop, return result. This string now contains the Excel column title corresponding to the original number.

Algorithm Walkthroug

Consider the input 705:

  1. Initialization: Start with n = 705 and an empty string result.

  2. Iteration 1:

    • Decrement n: n = 705 - 1 = 704.
    • Calculate 704 % 26 = 2, which corresponds to the third letter 'C' (since 'A' = 0, 'B' = 1, 'C' = 2 in our adjusted system).
    • Prepend 'C' to result, so result = "C".
    • Divide n by 26: 704 / 26 = 27.
  3. Iteration 2:

    • Decrement n: n = 27 - 1 = 26.
    • Calculate 26 % 26 = 0, corresponding to 'A' (after adjustment).
    • Prepend 'A' to result, so result = "AC".
    • Divide n by 26: 26 / 26 = 1.
  4. Iteration 3:

    • Decrement n: n = 1 - 1 = 0.
    • Since n is now 0, we would typically end, but the loop condition checks this at the start of each iteration. Before decrementing, n = 1, which corresponds to 'A'.
    • Prepend 'A' to result, making it "AAC".
    • The loop ends as n is now 0 after the division in the previous step.
  5. Result: The final result string is "AAC", which is the correct Excel column title for the number 705.

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

  • Time Complexity: , where n is the input number. This is because with each iteration, we divide the input number by 26, effectively reducing the problem size logarithmically.

  • Space Complexity: , as the space used by the algorithm is constant, and it only requires a finite number of variables regardless of the input size.

Excel Sheet Column Title (easy)
Merge Strings Alternately (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-step Algorithm

Algorithm Walkthroug

Code

Complexity Analysis